import axios from 'axios'

// 配置基础URL
const API_BASE_URL = 'http://localhost:8081'

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 学习进度API
export const progressAPI = {
  // 更新章节进度
  updateChapterProgress: (progressData) => api.post('/api/progress/chapters', progressData),
  
  // 更新资料进度
  updateMaterialProgress: (progressData) => api.post('/api/progress/materials', progressData),
  
  // 获取用户章节进度
  getUserChapterProgress: (userId, courseId) => api.get(`/api/progress/chapters/users/${userId}/courses/${courseId}`),
  
  // 获取用户资料进度
  getUserMaterialProgress: (userId, courseId) => api.get(`/api/progress/materials/users/${userId}/courses/${courseId}`),
  
  // 获取用户整体进度
  getUserOverallProgress: (userId, courseId) => api.get(`/api/progress/overall/users/${userId}/courses/${courseId}`),
  
  // 获取课程进度
  getCourseProgress: (userId, courseId) => api.get(`/api/progress/courses/${courseId}/users/${userId}`),
  
  // 获取章节进度
  getChapterProgress: (userId, chapterId) => api.get(`/api/progress/chapters/${chapterId}/users/${userId}`),
  
  // 获取资料进度
  getMaterialProgress: (userId, materialId) => api.get(`/api/progress/materials/${materialId}/users/${userId}`),
  
  // 批量获取章节进度
  getBatchChapterProgress: (userId, chapterIds) => api.post(`/api/progress/chapters/batch/users/${userId}`, chapterIds),
  
  // 批量获取资料进度
  getBatchMaterialProgress: (userId, materialIds) => api.post(`/api/progress/materials/batch/users/${userId}`, materialIds),
  
  // 获取班级完成率
  getClassCompletionRate: (classId) => api.get(`/api/progress/classes/${classId}/completion-rate`),
  
  // 获取练习完成率
  getExerciseCompletionRate: (exerciseId) => api.get(`/api/progress/exercises/${exerciseId}/completion-rate`),
  
  // 获取已完成章节ID列表
  getCompletedChapterIds: (userId, courseId) => api.get(`/api/progress/chapters/completed/users/${userId}/courses/${courseId}`),
  
  // 获取已完成资料ID列表
  getCompletedMaterialIds: (userId, courseId) => api.get(`/api/progress/materials/completed/users/${userId}/courses/${courseId}`),
  
  // 标记章节完成
  markChapterAsComplete: (userId, chapterId) => api.post(`/api/progress/chapters/${chapterId}/complete/users/${userId}`),
  
  // 标记资料完成
  markMaterialAsComplete: (userId, materialId) => api.post(`/api/progress/materials/${materialId}/complete/users/${userId}`),
  
  // 获取已完成练习ID列表
  getCompletedExerciseIds: (userId) => api.get(`/api/progress/exercises/completed/users/${userId}`),
  
  // 获取未完成练习ID列表
  getUnfinishedExerciseIds: (userId) => api.get(`/api/progress/exercises/unfinished/users/${userId}`),
  
  // 根据课程获取已完成练习
  getCompletedExerciseIdsByCourse: (userId, courseId) => api.get(`/api/progress/exercises/completed/users/${userId}/courses/${courseId}`),
  
  // 根据课程获取未完成练习
  getUnfinishedExerciseIdsByCourse: (userId, courseId) => api.get(`/api/progress/exercises/unfinished/users/${userId}/courses/${courseId}`),
  
  // 标记练习完成
  markExerciseAsComplete: (userId, exerciseId, score) => api.post(`/api/progress/exercises/${exerciseId}/complete/users/${userId}`, { score }),
  
  // 检查练习是否完成
  isExerciseCompleted: (userId, exerciseId) => api.get(`/api/progress/exercises/${exerciseId}/completed/users/${userId}`),
  
  // 获取学生练习状态
  getStudentExerciseStatus: (userId, exerciseId) => api.get(`/api/progress/exercises/${exerciseId}/status/users/${userId}`),
  
  // 更新学生练习状态
  updateStudentExerciseStatus: (userId, exerciseId, isFinished, score) => api.put(`/api/progress/exercises/${exerciseId}/status/users/${userId}`, { isFinished, score }),
  
  // 获取学生在章节中已完成的资料
  getCompletedMaterialsByStudentAndChapter: (userId, chapterId) => api.get(`/api/progress/materials/completed/users/${userId}/chapters/${chapterId}`),
  
  // 更新学生资料学习状态
  updateStudentMaterialStatus: (userId, materialId, isFinished) => api.put(`/api/progress/materials/${materialId}/status/users/${userId}`, { isFinished }),
  
  // 获取学生资料学习状态
  getStudentMaterialStatus: (userId, materialId) => api.get(`/api/progress/materials/${materialId}/status/users/${userId}`)
}

export default api
